﻿using System;
using System.Collections.Generic;
using System.Text;

using CMN.BLL;
using CMN.Entity;
using CMN.Cache;
using CMN.Common;
using CMN.Config;
namespace CMN.Framework
{
    /// <summary>
    ///  缓存管理
    /// </summary>
    public class MvcWebCacheManager
    {

        #region 专题
        /// <summary>
        /// 把不同模板列表转换成通用文章显示列表
        /// </summary>
        /// <param name="classType"></param>
        /// <param name="qp"></param>
        /// <param name="cacheSecends"></param>
        /// <returns></returns>
        public static List<Normal_ZTEntity> GetZTList( QueryParam qp, int cacheSecends)
        {
            List<Normal_ZTEntity> ztLists = new List<Normal_ZTEntity>();
            Normal_ZT ztBll = new Normal_ZT();
            int recordCount;
            if (cacheSecends <= 0)
            {
                ztLists = ztBll.List(qp, out recordCount);

            }
            else
            {
                string path = string.Concat(Utils.GetServerPath(), "App_Data\\cache\\zt\\");
                string name = string.Concat(qp.Where, qp.Orderfld, qp.PageSize.ToString(), qp.PageIndex.ToString());
                name = Utils.Md5(name, 32);
                name = string.Concat(name, ".htm");
                string key = string.Concat(path, name);
                JsonFileCache<Normal_ZTEntity> jFileCache = new JsonFileCache<Normal_ZTEntity>();
                if (jFileCache.IsOutTime(key))
                {
                    ztLists = ztBll.List(qp, out recordCount);
                    jFileCache.Insert(key, ztLists, cacheSecends, qp.IsTimeSearch);
                    return ztLists;
                }
                else
                    ztLists = (List<Normal_ZTEntity>)jFileCache[key];
            }


            return ztLists;
        }
        #endregion
        #region  频道
        /// <summary>
        /// 缓存中获取频道列表
        /// </summary>
        /// <returns></returns>
        public static List<Normal_ChannelEntity> GetNormal_ChannelList()
        {
           
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = Int32.MaxValue;
            qp.Orderfld = " ordernum asc";
            qp.Where = " Enabled=1 ";
            List<Normal_ChannelEntity> czflList = GetNormal_ChannelList(qp);
            return czflList;
        }

        /// <summary>
        /// 缓存中获取频道列表
        /// </summary>
        /// <returns></returns>
        public static List<Normal_ChannelEntity> GetNormal_ChannelList(QueryParam qp)
        {
            HttpWebCache cache = new HttpWebCache();
            if (cache[string.Concat(CacheKeys.MVC_Normal_ChannelList, qp.Where.Replace(" ", ""), qp.Orderfld.Replace(" ", ""))] == null)//
            {
                SiteConfigInfo siteInfo = SiteConfigs.GetSiteConfig();
                int recordCount = 0;
                Normal_Channel bll = new Normal_Channel();
                List<Normal_ChannelEntity> czflList = bll.List(qp, out recordCount);
                SiteConfigInfo site = SiteConfigs.GetSiteConfig();
                string tempUrl = string.Empty;
                foreach (Normal_ChannelEntity channel in czflList)
                {
                    tempUrl = (string.IsNullOrEmpty(siteInfo.ChannelUrl)?channel.Dir: string.Format(siteInfo.ChannelUrl, channel.Dir));
                    if (channel.IsPart != 2) //是否外部频道
                    {
                        if (channel.ReId == 0)
                        {
                            if (channel.IsPart == 3)
                            {
                                channel.Url = string.Concat("http://",  tempUrl);
                                channel.DirectoryUrl = string.Concat("http://", channel.Dir);
                            }
                            else
                            {
                                channel.Url = string.Concat(site.Dir, "/", tempUrl);
                                channel.DirectoryUrl = string.Concat(site.Dir, "/", channel.Dir);

                            }
                        }
                        else
                        {
                            foreach (Normal_ChannelEntity channel2 in czflList)
                            {
                                if (channel2.Id == channel.ReId)
                                {
                                    if (channel2.ReId == 0)
                                    {
                                        if (channel2.IsPart == 3)
                                        {
                                            channel.Url = string.Concat("http://", channel2.Dir, "/", tempUrl);
                                            channel.DirectoryUrl = string.Concat("http://", channel2.Dir, "/", channel.Dir);
                                        }
                                        else
                                        {
                                            channel.Url = string.Concat(site.Dir, "/", channel2.Dir, "/", tempUrl);
                                            channel.DirectoryUrl = string.Concat(site.Dir, "/", channel2.Dir, "/", channel.Dir);
                                        }
                                        break;
                                    }
                                    else
                                    {
                                        foreach (Normal_ChannelEntity channel3 in czflList)
                                        {
                                            if (channel3.Id == channel2.ReId)
                                            {
                                                if (channel3.ReId == 0)
                                                {
                                                    if (channel3.IsPart == 3)
                                                    {
                                                        channel.Url = string.Concat("http://", channel3.Dir, "/", tempUrl);
                                                        channel.DirectoryUrl = string.Concat("http://", channel3.Dir, "/", channel.Dir);
                                                    }
                                                    else
                                                    {
                                                        channel.Url = string.Concat(site.Dir, "/", channel3.Dir, "/", tempUrl);
                                                        channel.DirectoryUrl = string.Concat(site.Dir, "/", channel3.Dir, "/", channel.Dir);
                                                    }

                                                }
                                                else
                                                {
                                                    foreach (Normal_ChannelEntity channel4 in czflList)
                                                    {
                                                        if (channel4.Id == channel3.ReId)
                                                        {
                                                            if (channel4.ReId == 0)
                                                            {
                                                                if (channel4.IsPart == 3)
                                                                {
                                                                    channel.Url = string.Concat("http://", channel4.Dir, "/", tempUrl);
                                                                    channel.DirectoryUrl = string.Concat("http://", channel4.Dir, "/", channel.Dir);
                                                                }
                                                                else
                                                                {
                                                                    channel.Url = string.Concat(site.Dir, "/", channel4.Dir, "/", tempUrl);
                                                                    channel.DirectoryUrl = string.Concat(site.Dir, "/", channel4.Dir, "/", channel.Dir);
                                                                }
                                                            }
                                                            else
                                                            {
                                                                foreach (Normal_ChannelEntity channel5 in czflList)
                                                                {
                                                                    if (channel5.Id == channel4.ReId)
                                                                    {
                                                                        if (channel5.ReId == 0)
                                                                        {
                                                                            if (channel5.IsPart == 3)
                                                                            {
                                                                                channel.Url = string.Concat("http://", channel5.Dir, "/", tempUrl);
                                                                                channel.DirectoryUrl = string.Concat("http://", channel5.Dir, "/", channel.Dir);
                                                                            }
                                                                            else
                                                                            {
                                                                                channel.Url = string.Concat(site.Dir, "/", channel5.Dir, "/", tempUrl);
                                                                                channel.DirectoryUrl = string.Concat(site.Dir, "/", channel5.Dir, "/", channel.Dir);
                                                                            }
                                                                        }
                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                            break;
                                                        }
                                                    }
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        channel.Url = channel.Dir;
                    }
                }
                cache.Insert(string.Concat(CacheKeys.MVC_Normal_ChannelList, qp.Where.Replace(" ", ""), qp.Orderfld.Replace(" ", "")), czflList, WebAppCacheConfigs.GetConfig().GetWebAppCacheInfo(CacheKeys.MVC_Normal_ChannelList).CacheTime+60);
            }
            return (List<Normal_ChannelEntity>)cache[string.Concat(CacheKeys.MVC_Normal_ChannelList, qp.Where.Replace(" ", ""), qp.Orderfld.Replace(" ", ""))];
        }
        
        /// <summary>
        /// 获取频道列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<Normal_ChannelEntity> GetNormal_ChannelList(int id,int retrunCount)
        {
            List<Normal_ChannelEntity> czflList = GetNormal_ChannelList();
            Normal_ChannelEntity channel = new Normal_ChannelEntity();
            List<Normal_ChannelEntity> reList = new List<Normal_ChannelEntity>();
            for (int i = 0; i < czflList.Count; i++)
            {
                if (czflList[i].ReId == id)
                {
                   
                    reList.Add(czflList[i]);
                    if (reList.Count == retrunCount)
                        break;
                    
                }
            }
            return reList;
        }
        /// <summary>
        /// 获取频道列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<Normal_ChannelEntity> GetNormal_ChannelList2(int id)
        {
            List<Normal_ChannelEntity> czflList = GetNormal_ChannelList();
            
            List<Normal_ChannelEntity> reList = new List<Normal_ChannelEntity>();
            for (int i = 0; i < czflList.Count; i++)
            {
                if (czflList[i].ReId == id)
                {
                    reList.Add(czflList[i]);

                }
            }
            if (reList.Count < 1)
            {
                Normal_ChannelEntity channel = GetNormal_Channel(id);
                if (channel != null)
                {
                    for (int i = 0; i < czflList.Count; i++)
                    {
                        if (czflList[i].ReId == channel.ReId)
                        {
                            reList.Add(czflList[i]);

                        }
                    }
                }

            }
            return reList;
        }

        /// <summary>
        /// 获取推荐频道列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<Normal_ChannelEntity> GetNormal_ChannelList(string channelPath, bool isRecommend, int count)
        {
            List<Normal_ChannelEntity> czflList = GetNormal_ChannelList();
            Normal_ChannelEntity channel = new Normal_ChannelEntity();
            List<Normal_ChannelEntity> reList = new List<Normal_ChannelEntity>();
            for (int i = 0; i < czflList.Count; i++)
            {
                if ((!isRecommend|| czflList[i].OrderNum < 10 )&& (string.IsNullOrEmpty(channelPath) || czflList[i].Path.StartsWith(channelPath)))
                {
                    
                    reList.Add(czflList[i]);
                    if (reList.Count == count)
                        return reList;
                }
            }
            return reList;
        }

        /// <summary>
        ///  获取频道
        /// </summary>
        /// <param name="bh"></param>
        /// <returns></returns>
        public static Normal_ChannelEntity GetNormal_Channel(int id)
        {
            List<Normal_ChannelEntity> czflList = GetNormal_ChannelList();
            Normal_ChannelEntity channel = null;
            for (int i = 0; i < czflList.Count; i++)
            {
                if (czflList[i].Id == id)
                {
                    channel = czflList[i];
                    break;
                }
            }
            return channel;
        }

        /// <summary>
        ///  获取频道
        /// </summary>
        /// <param name="dir">频道地址</param>
        /// <returns></returns>
        public static Normal_ChannelEntity GetNormal_ChannelByDir(string dir)
        {
            List<Normal_ChannelEntity> czflList = GetNormal_ChannelList();
            Normal_ChannelEntity channel = null;
            if (string.IsNullOrEmpty(dir))
            {
                return channel;
            }
            for (int i = 0; i < czflList.Count; i++)
            {
               
                
                    if (czflList[i].Dir.ToLower() == dir.ToLower())
                    {
                        channel = czflList[i];
                        break;
                    }
            }
            return channel;
        }

        /// <summary>
        ///  获取频道
        /// </summary>
        /// <param name="path">频道搜索路径</param>
        /// <returns></returns>
        public static Normal_ChannelEntity GetNormal_ChannelByPath(string path)
        {
            List<Normal_ChannelEntity> czflList = GetNormal_ChannelList();
            Normal_ChannelEntity channel = null;
            for (int i = 0; i < czflList.Count; i++)
            {

                if (czflList[i].Path.ToLower() == path.ToLower())
                    {
                        channel = czflList[i];
                        break;
                    }
            }
            return channel;
        }

        
        /// <summary>
        /// 移除频道
        /// </summary>
        public static void RemoveNormal_ChannelList()
        {
            HttpWebCache cache = new HttpWebCache();
            if (cache[CacheKeys.MVC_Normal_ChannelList] != null)
            {
                cache.Remove(CacheKeys.MVC_Normal_ChannelList);
            }
        }

        #endregion  

        #region 把不同模板列表转换成通用文章显示列表
        /// <summary>
        /// 把不同模板列表转换成通用文章显示列表
        /// </summary>
        /// <param name="classType"></param>
        /// <param name="qp"></param>
        /// <param name="cacheSecends"></param>
        /// <returns></returns>
        public static List<Module_ListInfo> GetModuleList(string classType, QueryParam qp,int cacheSecends,bool isReturnCountNum)
        {
            List<Module_ListInfo> Module_ListInfos = new List<Module_ListInfo>();
  
            if (cacheSecends <= 0)
            {
                Module_ListInfos = GetModuleList(classType, qp,isReturnCountNum);
            }
            else
            {
                string path = string.Concat(Utils.GetServerPath(), "App_Data\\cache\\Module_List\\",classType,"\\");
                string name = string.Concat( qp.Where, qp.Orderfld, qp.PageSize.ToString(), qp.PageIndex.ToString());
                name = Utils.Md5(name, 32);
                 name = string.Concat(name, ".htm");
                string key = string.Concat(path, name);
                JsonFileCache<Module_ListInfo> jFileCache=new JsonFileCache<Module_ListInfo>();
                Module_ListInfos = (List<Module_ListInfo>)jFileCache[key];
                if (jFileCache.IsOutTime(key))
                {
                    qp.ModuleType = classType;
                    qp.TimeTemp = cacheSecends;
                    QueryParam qpInsert = new QueryParam();
                    qpInsert = qp;
                    
                    ModuleListCache.Instance().InsertValue(key, qpInsert);
                   
                }
                
            }
            

            return Module_ListInfos;
        }

        /// <summary>
        /// 把不同模板列表转换成通用文章显示列表
        /// </summary>
        /// <param name="classType"></param>
        /// <param name="qp"></param>
        /// <returns></returns>
        public static List<Module_ListInfo> GetModuleList(string classType, QueryParam qp, bool isReturnCountNum)
        {
            List<Module_ListInfo> Module_ListInfos = new List<Module_ListInfo>();
            if (qp.PageSize == 0)
            {
                qp.PageSize = SiteConfigs.GetSiteConfig().PageSize;
            }
            if (string.IsNullOrEmpty(qp.ReturnFields))
            {
                qp.ReturnFields = "Id,ChannelId,Path ,Title,AddDate,Summary,Tags,Img ,SourceFrom,ViewNum,SupportNum,Enabled";
            }
            Module_ListInfo Module_ListInfo;
            classType = classType.ToLower();
            int recordCount = 0;
            #region 增加文章列表
            switch (classType)
            {
                case "article":
                    {
                        Module_Article article = new Module_Article();
                     
                        List<Module_ArticleEntity> articleList = article.List(qp, isReturnCountNum, out recordCount);
                        foreach (Module_ArticleEntity articleEntity in articleList)
                        {
                            Module_ListInfo = new Entity.Module_ListInfo();
                            Module_ListInfo.AddDate = articleEntity.AddDate;
                            Module_ListInfo.AgainstNum = articleEntity.AgainstNum;
                            Module_ListInfo.Author = articleEntity.Author;
                            Module_ListInfo.ChannelId = articleEntity.ChannelId;
                            Module_ListInfo.Flag = articleEntity.Flag;
                            Module_ListInfo.Id = articleEntity.Id;
                            Module_ListInfo.Img = articleEntity.Img;
                            Module_ListInfo.OrderNum = articleEntity.OrderNum;
                            Module_ListInfo.Path = articleEntity.Path;
                            Module_ListInfo.SourceFrom = articleEntity.SourceFrom;
                            Module_ListInfo.SupportNum = articleEntity.SupportNum;
                            Module_ListInfo.Summary = articleEntity.Summary;
                            Module_ListInfo.Tags = articleEntity.Tags;
                            Module_ListInfo.TColor = articleEntity.TColor;
                            Module_ListInfo.Title = articleEntity.Title;
                            Module_ListInfo.TopTime = articleEntity.TopTime;
                            Module_ListInfo.Url = articleEntity.Url;
                            Module_ListInfo.UserId = articleEntity.UserId;
                            Module_ListInfo.ViewNum = articleEntity.ViewNum;
                            Module_ListInfo.Enabled = articleEntity.Enabled;
                            Module_ListInfos.Add(Module_ListInfo);
                        }
                        articleList.Clear();
                        break;
                    }
                case "photo":
                    {
                        Module_Photo photo = new Module_Photo();
                        List<Module_PhotoEntity> articleList = photo.List(qp, isReturnCountNum, out  recordCount);
                        foreach (Module_PhotoEntity articleEntity in articleList)
                        {
                            Module_ListInfo = (Module_ListInfo)articleEntity;
                            Module_ListInfos.Add(Module_ListInfo);
                        }
                        articleList.Clear();
                        break;
                    }
                case "soft":
                    {
                        Module_Soft soft = new Module_Soft();
                        List<Module_SoftEntity> articleList = soft.List(qp, isReturnCountNum, out   recordCount);
                        foreach (Module_SoftEntity articleEntity in articleList)
                        {
                            Module_ListInfo = (Module_ListInfo)articleEntity;

                            Module_ListInfos.Add(Module_ListInfo);
                        }
                        articleList.Clear();
                        break;
                    }
                case "video":
                    {
                        Module_Video video = new Module_Video();
                        List<Module_VideoEntity> articleList = video.List(qp, isReturnCountNum, out  recordCount);
                        foreach (Module_VideoEntity articleEntity in articleList)
                        {
                            Module_ListInfo = (Module_ListInfo)articleEntity;
                            Module_ListInfos.Add(Module_ListInfo);
                        }
                        articleList.Clear();
                        break;
                    }
                default:
                    {
                        throw new Exception("栏目类别错误");

                    }
                    
            }


            SiteConfigInfo siteInfo = SiteConfigs.GetSiteConfig();
            Normal_ChannelEntity channel2 = null; ;
            foreach (Module_ListInfo listInfo in Module_ListInfos)
            {
                channel2 = new Normal_ChannelEntity();
                channel2 = GetNormal_ChannelByPath(listInfo.Path);
                if (channel2 != null)
                {
                    listInfo.Url = string.Concat(channel2.DirectoryUrl, "/", (string.IsNullOrEmpty(siteInfo.ChannelUrl) ? listInfo.Id.ToString() : string.Format(siteInfo.ChannelUrl, listInfo.Id.ToString())));
                }
            }
            qp.Count = recordCount;
            #endregion

            return Module_ListInfos;
        }
        #endregion

        #region 获取模板信息

        /// <summary>
        /// 获取模板详细信息
        /// </summary>
        /// <param name="templateId"></param>
        /// <returns></returns>
        public static List<Normal_TemplateListEntity> GetTemlateListList()
        {
            HttpWebCache cache = new HttpWebCache();
            List<Normal_TemplateListEntity> templateList = new List<Normal_TemplateListEntity>();

            if (cache[CacheKeys.MVC_TempliteInfoList] == null)
            {
                Normal_TemplateList bll = new Normal_TemplateList();
                QueryParam qp = new QueryParam();
                qp.PageIndex = 1;
                qp.PageSize = 1000;
                int recordCount = 0;
                templateList = bll.List(qp, out  recordCount);
                cache.Insert(CacheKeys.MVC_TempliteInfoList, templateList, 600);
            }



            return (List<Normal_TemplateListEntity>)cache[CacheKeys.MVC_TempliteInfoList];

        }

        /// <summary>
        /// 获取模板详细信息
        /// </summary>
        /// <param name="templateId"></param>
        /// <returns></returns>
        public static Normal_TemplateListEntity GetTemlateListEntity(int templateId)
        {
            List<Normal_TemplateListEntity> templateList = GetTemlateListList();
            Normal_TemplateListEntity rtemplate = null;
            foreach (Normal_TemplateListEntity template in templateList)
            {
                if (template.Id == templateId)
                {
                    rtemplate = template;
                    break;
                }
            }
            return rtemplate;

        }
       



        /// <summary>
        /// 获取模板信息
        /// </summary>
        /// <param name="templateId"></param>
        /// <returns></returns>
        public static List<Normal_TemplatesEntity> GetTemlateList()
        {
            HttpWebCache cache = new HttpWebCache();
            List<Normal_TemplatesEntity> templateList = new List<Normal_TemplatesEntity>();

            if (cache[CacheKeys.MVC_TempliteList] == null)
            {
                Normal_Templates bll = new Normal_Templates();
                QueryParam qp = new QueryParam();
                qp.PageIndex = 1;
                qp.PageSize = 1000;
                int recordCount = 0;
                templateList = bll.List(qp, out  recordCount);
                foreach (Normal_TemplatesEntity template in templateList)
                {
                    template.Dir = template.Dir;

                }
                cache.Insert(CacheKeys.MVC_TempliteList, templateList, 120);
            }



            return ( List<Normal_TemplatesEntity>)cache[CacheKeys.MVC_TempliteList];

        }

        /// <summary>
        /// 获取模板信息
        /// </summary>
        /// <param name="templateId"></param>
        /// <returns></returns>
        public static Normal_TemplatesEntity GetTemlateEntity(int templateId)
        {
            List<Normal_TemplatesEntity> templateList = GetTemlateList();
            Normal_TemplatesEntity rtemplate = null ;
            foreach (Normal_TemplatesEntity template in templateList)
            {
                if (template.Id == templateId)
                {
                    rtemplate = template;
                    break;
                }
            }
            return rtemplate;

        }
        /// <summary>
        /// 获取模板信息
        /// </summary>
        /// <param name="templateId"></param>
        /// <returns></returns>
        public static Normal_TemplatesEntity GetTemlateEntity(string dir)
        {
            List<Normal_TemplatesEntity> templateList = GetTemlateList();
            Normal_TemplatesEntity rtemplate = null;
            foreach (Normal_TemplatesEntity template in templateList)
            {
                if (template.Dir == dir)
                {
                    rtemplate = template;
                    break;
                }
            }
            return rtemplate;
               
           
        }



        /// <summary>
        /// 获取模板信息
        /// </summary>
        /// <returns></returns>
        public static Normal_TemplatesEntity GetDefaultTemlateEntity()
        {
            List<Normal_TemplatesEntity> templateList = GetTemlateList();
            Normal_TemplatesEntity rtemplate = null; 
            foreach (Normal_TemplatesEntity template in templateList)
            {
                if (template.IsDefault == true)
                {
                    rtemplate = template;
                    break;
                }
            }
            return rtemplate;


        }
        #endregion

        #region 获取模板样式信息
        /// <summary>
        /// 获取模板样式信息
        /// </summary>
        /// <param name="skinId"></param>
        /// <returns></returns>
        public static Normal_SkinEntity GetSkinEntity(int skinId)
        {
            HttpWebCache cache = new HttpWebCache();
            if (cache[CacheKeys.MVC_SkinList] == null)
            {
                Dictionary<int, Normal_SkinEntity> dic = new Dictionary<int, Normal_SkinEntity>();
                QueryParam qp = new QueryParam();
                qp.PageIndex = 1;
                qp.PageSize = 1000;
                int recordCount = 0;
                Normal_Skin skin = new Normal_Skin();
                List<Normal_SkinEntity> SkinList = skin.List(qp, out recordCount);
                foreach (Normal_SkinEntity Skin in SkinList)
                {
                    Skin.Dir = Skin.Dir.Replace('\\', '/');
                    if (!dic.ContainsKey(Skin.Id))
                    {
                        dic.Add(Skin.Id, Skin);
                    }
                }
                cache.Insert(CacheKeys.MVC_SkinList, dic, 120);
            }
            Dictionary<int, Normal_SkinEntity> dic2 = (Dictionary<int, Normal_SkinEntity>)cache[CacheKeys.MVC_SkinList];
            if (dic2.ContainsKey(skinId))
            {
                return dic2[skinId];
            }
            else
            {
                return null;
            }

        }
        #endregion

        #region 获取广告
        /// <summary>
        /// 把不同模板列表转换成通用文章显示列表
        /// </summary>
        /// <param name="classType"></param>
        /// <param name="qp"></param>
        /// <param name="cacheSecends"></param>
        /// <returns></returns>
        public static Normal_AdvertiseEntity GetNormal_Advertise(string code)
        {
            Normal_AdvertiseEntity adv = new Normal_AdvertiseEntity();

            Normal_Advertise bll = new Normal_Advertise();
           // string path = string.Concat(Utils.GetServerPath(), "App_Data\\cache\\Advertise\\");
           // string name = string.Concat(code, ".htm");
            string key = string.Concat("Advertise",code);
            HttpWebCache webCache = new HttpWebCache();
            if (webCache[key] == null)
            {
                adv = bll.Disp(code);
                if(adv!=null)
                webCache.Insert(key, adv,300);
            }
            else
            {
                adv = (Normal_AdvertiseEntity)webCache[key];
            }
            return adv;
        }


        #endregion

        #region 点击数列表
        /// <summary>
        /// 缓存中保存点击数列表
        /// </summary>
        /// <returns></returns>
        public static void SaveNormal_ClickRecordList(Normal_ClickRecordEntity clickRecordEntity)
        {
            HttpWebCache cache = new HttpWebCache();
            List<Normal_ClickRecordEntity> clickList =null;
            if (cache[CacheKeys.ClickCacheList] == null)
            {
                clickList=new List<Normal_ClickRecordEntity>();
                clickList.Add(clickRecordEntity);
                cache.Insert(CacheKeys.ClickCacheList, clickList);
            }
            else
            {
                clickList = (List<Normal_ClickRecordEntity>)cache[CacheKeys.ClickCacheList];
                if (clickList == null)
                    clickList = new List<Normal_ClickRecordEntity>();
                clickList.Add(clickRecordEntity);
                cache.Insert(CacheKeys.ClickCacheList, clickList);
            }
           
        }
        /// <summary>
        /// 获取缓存中保存点击数列表
        /// </summary>
        /// <returns></returns>
        public static List<Normal_ClickRecordEntity> GetNormal_ClickRecordList()
        {
            HttpWebCache cache = new HttpWebCache();
            List<Normal_ClickRecordEntity> clickList = null;
            if (cache[CacheKeys.ClickCacheList] != null)
            {
                clickList = (List<Normal_ClickRecordEntity>)cache[CacheKeys.ClickCacheList];

            }
            return clickList;
        }

        public static void RemoveNormal_ClickRecordList()
        {
            HttpWebCache cache = new HttpWebCache();
            if (cache[CacheKeys.ClickCacheList] != null)
            {
                cache.Remove(CacheKeys.ClickCacheList);
            }
        }
        #endregion 

        #region 更新搜索列表
        /// <summary>
        /// 缓存搜索列表
        /// </summary>
        /// <returns></returns>
        public static void SaveModule_ArticleSearchList(Module_ArticleEntity articleEntity)
        {
            HttpWebCache cache = new HttpWebCache();
            List<Module_ArticleEntity> clickList = null;
            if (cache[CacheKeys.ArticleSearchCacheList] == null)
            {
                clickList = new List<Module_ArticleEntity>();
                if (!clickList.Contains(articleEntity))
                {
                    clickList.Add(articleEntity);
                    cache.Insert(CacheKeys.ArticleSearchCacheList, clickList);
                }
            }
            else
            {
                clickList = (List<Module_ArticleEntity>)cache[CacheKeys.ArticleSearchCacheList];
                if (clickList == null)
                    clickList = new List<Module_ArticleEntity>();
                if (!clickList.Contains(articleEntity))
                {
                    clickList.Add(articleEntity);
                    cache.Insert(CacheKeys.ArticleSearchCacheList, clickList);
                }
            }

        }
        /// <summary>
        /// 获取缓存中保存点击数列表
        /// </summary>
        /// <returns></returns>
        public static List<Module_ArticleEntity> GetCacheModule_ArticleSearchList()
        {
            HttpWebCache cache = new HttpWebCache();
            List<Module_ArticleEntity> clickList = null;
            if (cache[CacheKeys.ArticleSearchCacheList] != null)
            {
                clickList = (List<Module_ArticleEntity>)cache[CacheKeys.ArticleSearchCacheList];

            }
            return clickList;
        }

        public static void RemoveModule_ArticleSearchList()
        {
            HttpWebCache cache = new HttpWebCache();
            if (cache[CacheKeys.ArticleSearchCacheList] != null)
            {
                cache.Remove(CacheKeys.ArticleSearchCacheList);
            }
        }
        #endregion 
        #region 获取分词器

     

    
        /// <summary>
        /// 获取分词器
        /// </summary>
        /// <returns>FastFilter</returns>
        public static Common.Filter.FastFilter GetBadWordFastFilter()
        {

            HttpWebCache cache = new HttpWebCache();
            Common.Filter.FastFilter ff = new Common.Filter.FastFilter();
          

            string key = "GetBadWordFastFilter";

         
            if (cache[key] == null)
            {
                string str = Utils.GetFileContent(Utils.GetServerPath() + "App_Data/data/badword.txt", Encoding.GetEncoding("gb2312"));
                string[] strs = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < strs.Length; i++)
                {
                    if (strs[i].Length < 9)
                        ff.AddKey(strs[i]);
                }
                cache.Insert(key, ff, 3800,System.Web.Caching.CacheItemPriority.High);

            }
            else
            {
                ff = (Common.Filter.FastFilter)cache[key];
            }
            return ff;
        }

        /// <summary>
        /// 获取分词器
        /// </summary>
        /// <returns>FastFilter</returns>
        public static Common.Filter.FastFilter GetFastFilter()
        {

            List<Normal_KeywordEntity> keys = new List<Normal_KeywordEntity>();

            // string path = string.Concat(Utils.GetServerPath(), "App_Data\\cache\\Keyword\\");
            string name = string.Concat("Keyword.htm");
            string key = string.Concat(name);

            HttpWebCache cache = new HttpWebCache();
            Common.Filter.FastFilter ff = new Common.Filter.FastFilter();
            if (cache[key] == null)
            {
                Normal_Keyword bll = new Normal_Keyword();
                QueryParam qp = new QueryParam();
                int recordCount = 0;
                keys = bll.List(qp, out recordCount);

                SiteConfigInfo siteInfo = SiteConfigs.GetSiteConfig();
                foreach (Normal_KeywordEntity keyword in keys)
                {
                    if (keyword.Keyword.Length < 17)
                        ff.AddKey(keyword.Keyword, string.Concat("<a href=", keyword.KeyWordUrl, " target=_blank >{0}</a>"));
                }

                //栏目
                List<Normal_ChannelEntity> channelList = MvcWebCacheManager.GetNormal_ChannelList();
                foreach (Normal_ChannelEntity ch in channelList)
                {
                    if (ch.Title.Length > 2 && ch.Title.Length < 17 && ch.Title != "其他")
                    {
                        if (!string.IsNullOrEmpty(ch.Url))
                            ff.AddKey(ch.Title, string.Concat("<a   target=_blank   href=", (ch.Url.ToLower().IndexOf("www.") > -1 ? "" : siteInfo.Url), ch.Url, "  >{0}</a>"));
                    }
                }
                //tag
              
                List<Normal_TagsEntity> tagList =MvcWebCacheManager.GetNormal_TagsList();
                foreach (Normal_TagsEntity tag in tagList)
                {
                    if (tag.TagName.Length < 17)
                        ff.AddKey(tag.TagName, string.Concat("<a target=_blank  href=", (tag.Url.ToLower().IndexOf("www.") > -1 ? "" : siteInfo.Url), tag.Url, "   >{0}</a>"));
                }
                cache.Insert(key, ff, 3800,System.Web.Caching.CacheItemPriority.High);

            }
            else
            {
                ff = (Common.Filter.FastFilter)cache[key];
            }
            return ff;
        }


        #endregion

        #region 公告
        /// <summary>
        /// 公告列表
        /// </summary>
        /// <param name="qp"></param>
        /// <param name="cacheSecends"></param>
        /// <returns></returns>
        public static List<Normal_NoticeEntity> GetNormal_NoticeList(QueryParam qp, int cacheSecends)
        {
            List<Normal_NoticeEntity> NoticeListInfos = new List<Normal_NoticeEntity>();
            Normal_Notice bll = new Normal_Notice();
            int recordCount = 0;
            if (cacheSecends <= 0)
            {

                NoticeListInfos = bll.List(qp, out recordCount);
            }
            else
            {

                string path = string.Concat(Utils.GetServerPath(), "App_Data\\cache\\Normal_Notice\\");
                string name = string.Concat(qp.Orderfld.Replace(" ", ""), qp.PageSize.ToString(), qp.PageIndex.ToString(), ".htm");
                string key = string.Concat(path, name);
                JsonFileCache<Normal_NoticeEntity> jFileCache = new JsonFileCache<Normal_NoticeEntity>();
                if (jFileCache.IsOutTime(key))
                {
                    NoticeListInfos = bll.List(qp, out recordCount);
                    jFileCache.Insert(key, NoticeListInfos, cacheSecends);
                }
                else
                    NoticeListInfos = (List<Normal_NoticeEntity>)jFileCache[key];
            }


            return NoticeListInfos;
        }
        #endregion 

        #region 广告
        /// <summary>
        /// 广告列表
        /// </summary>
        /// <param name="qp"></param>
        /// <param name="cacheSecends"></param>
        /// <returns></returns>
        public static List<Normal_AdvertiseEntity> GetNormal_AdvertiseList(QueryParam qp, int cacheSecends)
        {
            List<Normal_AdvertiseEntity> NoticeListInfos = new List<Normal_AdvertiseEntity>();
            Normal_Advertise bll = new Normal_Advertise();
            int recordCount = 0;
            if (cacheSecends <= 0)
            {

                NoticeListInfos = bll.List(qp, out recordCount);
            }
            else
            {

                string path = string.Concat(Utils.GetServerPath(), "App_Data\\cache\\Normal_Advertise\\");
                string name = string.Concat(qp.Orderfld.Replace(" ", ""), qp.PageSize.ToString(), qp.PageIndex.ToString(), ".htm");
                string key = string.Concat(path, name);
                JsonFileCache<Normal_AdvertiseEntity> jFileCache = new JsonFileCache<Normal_AdvertiseEntity>();
                if (jFileCache.IsOutTime(key))
                {
                    NoticeListInfos = bll.List(qp, out recordCount);
                    jFileCache.Insert(key, NoticeListInfos, cacheSecends);
                }
                else
                    NoticeListInfos = (List<Normal_AdvertiseEntity>)jFileCache[key];
            }


            return NoticeListInfos;
        }
        #endregion 


        #region 友情链接
        /// <summary>
        ///友情链接列表
        /// </summary>
        /// <param name="qp"></param>
        /// <param name="cacheSecends"></param>
        /// <returns></returns>
        public static List<Normal_LinkEntity> GetNormal_LinkList(QueryParam qp, int cacheSecends)
        {
            List<Normal_LinkEntity> LinkListInfos = new List<Normal_LinkEntity>();
            Normal_Link bll = new Normal_Link();
            int recordCount = 0;
            if (cacheSecends <= 0)
            {

                LinkListInfos = bll.List(qp, out recordCount);
            }
            else
            {

                string path = string.Concat(Utils.GetServerPath(), "App_Data\\cache\\Normal_Link\\");
                string name = string.Concat(qp.Orderfld.Replace(" ", ""), qp.PageSize.ToString(), qp.PageIndex.ToString(), ".htm");
                string key = string.Concat(path, name);
                JsonFileCache<Normal_LinkEntity> jFileCache = new JsonFileCache<Normal_LinkEntity>();
                if (jFileCache.IsOutTime(key))
                {
                    LinkListInfos = bll.List(qp, out recordCount);
                    jFileCache.Insert(key, LinkListInfos, cacheSecends);
                }
                else
                    LinkListInfos = (List<Normal_LinkEntity>)jFileCache[key];
            }


            return LinkListInfos;
        }
        #endregion 

        #region 搜索内容


        /// <summary>
        ///搜索内容
        /// </summary>
        /// <param name="qp"></param>
        /// <param name="cacheSecends"></param>
        /// <returns></returns>
        public static List<Normal_SearchContentEntity> GetNormal_SearchContentList(QueryParam qp, int cacheSecends)
        {
            Normal_SearchContent bll = new Normal_SearchContent();
            List<Normal_SearchContentEntity> SearchContentInfos = new List<Normal_SearchContentEntity>();
            int recordCount = 0;
            if (cacheSecends <= 0)
            {

                SearchContentInfos = bll.List(qp, out recordCount);
                qp.Count = recordCount;
            }
            else
            {

                string path = string.Concat(Utils.GetServerPath(), "App_Data\\cache\\Normal_SearchContent\\");
                string name = string.Concat(qp.Where.Replace(" ", ""), qp.Orderfld.Replace(" ", ""), qp.PageSize.ToString(), qp.PageIndex.ToString(), ".htm");
                string key = string.Concat(path, name);
                JsonFileCache<Normal_SearchContentEntity> jFileCache = new JsonFileCache<Normal_SearchContentEntity>();
                if (jFileCache.IsOutTime(key))
                {
                    SearchContentInfos = bll.List(qp, out recordCount);
                    jFileCache.Insert(key, SearchContentInfos, cacheSecends);
                }
                else
                    SearchContentInfos = (List<Normal_SearchContentEntity>)jFileCache[key];
            }


            return SearchContentInfos;
        }
        #endregion 


        #region 搜索关键字
        /// <summary>
        ///搜索关键字
        /// </summary>
        /// <param name="qp"></param>
        /// <param name="cacheSecends"></param>
        /// <returns></returns>
        public static List<Normal_KeywordEntity> GetNormal_KeywordList(QueryParam qp, int cacheSecends)
        {
            List<Normal_KeywordEntity> SearchContentInfos = new List<Normal_KeywordEntity>();
            Normal_Keyword bll = new Normal_Keyword();
            int recordCount = 0;
            if (cacheSecends <= 0)
            {

                SearchContentInfos = bll.List(qp, out recordCount);
            }
            else
            {

                string path = string.Concat(Utils.GetServerPath(), "App_Data\\cache\\Normal_Keyword\\");
                string name = string.Concat(qp.Where.Replace(" ", ""), qp.Orderfld.Replace(" ", ""), qp.PageSize.ToString(), qp.PageIndex.ToString(), ".htm");
                string key = string.Concat(path, name);
                JsonFileCache<Normal_KeywordEntity> jFileCache = new JsonFileCache<Normal_KeywordEntity>();
                if (jFileCache.IsOutTime(key))
                {
                    SearchContentInfos = bll.List(qp, out recordCount);
                    jFileCache.Insert(key, SearchContentInfos, cacheSecends);

                }
                else
                    SearchContentInfos = (List<Normal_KeywordEntity>)jFileCache[key];
            }


            return SearchContentInfos;
        }
        #endregion 

        #region 获取标签
        /// <summary>
        /// 获取标签 列表
        /// </summary>
        /// <returns></returns>
        public static List<Normal_TagsEntity> GetNormal_TagsList()
        {

            List<Normal_TagsEntity> tagsList = new List<Normal_TagsEntity>();
            Normal_Tags bll = new Normal_Tags();
            string key = "Normal_TagsList";
            HttpWebCache cache = new HttpWebCache();
            if (cache[key] == null)
            {
                QueryParam qp = new QueryParam();
                qp.PageSize = Int32.MaxValue;
                qp.Orderfld = " ordernum desc ";
                int recordNum=0;
                tagsList = bll.List(qp,out recordNum);
                cache.Insert(key, tagsList,400);
            }
            else
            {
                tagsList = (List<Normal_TagsEntity>)cache[key];
            }
            return tagsList;
        }

        /// <summary>
        /// 获取标签详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Normal_TagsEntity GetNormal_Tags(int id)
        {
            Normal_TagsEntity tags = null;
            List<Normal_TagsEntity> list=GetNormal_TagsList();
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Id == id)
                {
                    tags = list[i];
                    break;
                }
            }
            return tags;
        }
        /// <summary>
        /// 获取标签详细信息
        /// </summary>
        /// <param name="tagName"></param>
        /// <returns></returns>
        public static Normal_TagsEntity GetNormal_Tags(string tagName)
        {
            Normal_TagsEntity tags = null;
            List<Normal_TagsEntity> list = GetNormal_TagsList();
            for (int i = 0; i < list.Count; i++)
            {
                
                if (list[i].TagName == tagName ||  list[i].Dir == tagName)
                {
                    tags = list[i];
                    break;
                }
            }
            return tags;
        }

        #endregion

        #region 投票
        /// <summary>
        ///搜索关键字
        /// </summary>
        /// <param name="qp"></param>
        /// <param name="cacheSecends"></param>
        /// <returns></returns>
        public static List<Normal_VoteEntity> GetNormal_VoteList(QueryParam qp, int cacheSecends)
        {
            List<Normal_VoteEntity> SearchContentInfos = new List<Normal_VoteEntity>();
            Normal_Vote bll = new Normal_Vote();
            int recordCount = 0;
            if (cacheSecends <= 0)
            {

                SearchContentInfos = bll.List(qp, out recordCount);
            }
            else
            {

                string path = string.Concat(Utils.GetServerPath(), "App_Data\\cache\\Normal_Vote\\");
                string name = string.Concat(qp.Orderfld.Replace(" ", ""), qp.PageSize.ToString(), qp.PageIndex.ToString(), ".htm");
                string key = string.Concat(path, name);
                JsonFileCache<Normal_LinkEntity> jFileCache = new JsonFileCache<Normal_LinkEntity>();
                if (jFileCache.IsOutTime(key))
                {
                    SearchContentInfos = bll.List(qp, out recordCount);
                    jFileCache.Insert(key, SearchContentInfos, cacheSecends);
                }
                else
                    SearchContentInfos = (List<Normal_VoteEntity>)jFileCache[key];
            }


            return SearchContentInfos;
        }
        #endregion 
    }
}
